<?php

/**
 * This is the model class for table "{{product}}".
 *
 * The followings are the available columns in table '{{product}}':
 * @property integer $id
 * @property string $name
 * @property integer $category_id
 * @property string $model
 * @property string $producer
 * @property string $number
 * @property string $specification
 * @property string $thumb
 * @property string $introduce
 */
class Product extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Product the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{product}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('name, category_id', 'required'),
            array('category_id', 'numerical', 'integerOnly' => true),
            array('name, model, producer, number, specification', 'length', 'max' => 128),
            array('thumb', 'file', 'types' => 'jpeg, jpg, gif, png', 'allowEmpty' => TRUE),
            array('introduce', 'safe'),
            array('id, name, category_id, model, producer, number, specification, thumb, introduce', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'category' => array(self::BELONGS_TO, 'ProductCategory', 'category_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => '名称',
            'category_id' => '分类',
            'model' => '型号',
            'producer' => '生产商',
            'number' => '编号',
            'specification' => '规格',
            'thumb' => '图片',
            'introduce' => '简介',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('model', $this->model, true);
        $criteria->compare('producer', $this->producer, true);
        $criteria->compare('number', $this->number, true);
        $criteria->compare('specification', $this->specification, true);
        $criteria->compare('thumb', $this->thumb, true);
        $criteria->compare('introduce', $this->introduce, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function getUrl() {
        return Yii::app()->createUrl('product/view', array(
                    'id' => $this->id,
                        //'title' => $this->title,
                ));
    }

}